package com.cloud.generator.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cloud.common.constant.GenConstants;
import com.cloud.domain.BaseEntity;
import com.cloud.utils.StringUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;

import java.util.Arrays;

/**
 * 代码生成业务字段表 gen_table_column
 *
 * @author yzj
 */
@Data
@Schema(name = "GenTableColumn对象", description = "代码生成")
@TableName("gen_table_column")
public class GenTableColumn extends BaseEntity {
    private static final long serialVersionUID = 1L;

    @Schema(description = "主键")
    @TableId(value = "column_id", type = IdType.AUTO)
    private Long columnId;

    @Schema(description = "归属表编号")
    private Long tableId;

    @Schema(description = "归属表列名称")
    private String columnName;

    @Schema(description = "归属表列描述")
    private String columnComment;

    @Schema(description = "归属表列类型")
    private String columnType;

    @Schema(description = "JAVA类型")
    private String javaType;

    @Schema(description = "JAVA字段名")
    @NotBlank(message = "Java属性不能为空")
    private String javaField;

    @Schema(description = "是否主键(1是)")
    private String isPk;

    @Schema(description = "是否自增(1是)")
    private String isIncrement;

    @Schema(description = "是否必填(1是)")
    private String isRequired;

    @Schema(description = "是否为插入字段(1是)")
    private String isInsert;

    @Schema(description = "是否编辑字段(1是)")
    private String isEdit;

    @Schema(description = "是否列表字段(1是)")
    private String isList;

    @Schema(description = "是否查询字段(1是)")
    private String isQuery;

    @Schema(description = "查询方式(EQ等于、NE不等于、GT大于、LT小于、LIKE模糊、BETWEEN范围)")
    private String queryType;

    /**
     * 显示类型（input文本框、textarea文本域、select下拉框、checkbox复选框、radio单选框、
     * datetime日期控件、image图片上传控件、upload文件上传控件、editor富文本控件）
     */
    @Schema(description = "显示类型")
    private String htmlType;

    @Schema(description = "字典类型")
    private String dictType;

    @Schema(description = "排序")
    private Integer sort;

    public boolean checkPk() {
        return checkPk(this.isPk);
    }

    public boolean checkPk(String isPk) {
        return isPk != null && StringUtils.equals(GenConstants.REQUIRE, isPk);
    }

    public boolean checkIncrement() {
        return checkIncrement(this.isIncrement);
    }

    public boolean checkIncrement(String isIncrement) {
        return isIncrement != null && StringUtils.equals(GenConstants.REQUIRE, isIncrement);
    }

    public boolean checkRequired() {
        return checkRequired(this.isRequired);
    }

    public boolean checkRequired(String isRequired) {
        return isRequired != null && StringUtils.equals(GenConstants.REQUIRE, isRequired);
    }

    public boolean checkInsert() {
        return checkInsert(this.isInsert);
    }

    public boolean checkInsert(String isInsert) {
        return isInsert != null && StringUtils.equals(GenConstants.REQUIRE, isInsert);
    }

    public boolean checkEdit() {
        return checkInsert(this.isEdit);
    }

    public boolean checkEdit(String isEdit) {
        return isEdit != null && StringUtils.equals(GenConstants.REQUIRE, isEdit);
    }

    public boolean checkList() {
        return checkList(this.isList);
    }

    public boolean checkList(String isList) {
        return isList != null && StringUtils.equals(GenConstants.REQUIRE, isList);
    }

    public boolean isSuperColumn() {
        return isSuperColumn(this.javaField);
    }

    public static boolean isSuperColumn(String javaField) {
        // 忽略Domain属性
        if (Arrays.asList(GenConstants.BASE_ENTITY).contains(javaField)) {
            return true;
        } else {
            return StringUtils.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY);
        }
    }

}
